package com.lovo.util;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.lovo.vo.TopUpVO;

public class Verifier {
    private static  final String TOKEN_SECRET="lovo";
    public static String verifierJwt(String token){
        //私钥及加密算法
        Algorithm algorithm = Algorithm.HMAC256(TOKEN_SECRET);
        String userName="";
        JWTVerifier verifier = JWT.require(algorithm).build();
        try {
            DecodedJWT jwt = verifier.verify(token); //认证token
            userName=jwt.getClaim("info").asString();//取token中的信息
        }catch (Exception e){
            return  userName;
        }
        return userName;
    }

    /**
     * 验证充值
     * @param token
     * @return
     */
    public static TopUpVO verifierPayJwt(String token){
        //私钥及加密算法
        Algorithm algorithm = Algorithm.HMAC256(TOKEN_SECRET);
        String userCode="";
        double money=0;
        TopUpVO vo=new TopUpVO();
        JWTVerifier verifier = JWT.require(algorithm).build();
        try {
            DecodedJWT jwt = verifier.verify(token); //认证token
            userCode=jwt.getClaim("userCode").asString();//取token中的信息
            money=jwt.getClaim("money").asDouble();
            vo.setMoney(money);
            vo.setUserCode(userCode);
        }catch (Exception e){
            return  null;
        }
        return vo;
    }
}
